package com.rytx.system.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayOpenAppQrcodeCreateModel;
import com.alipay.api.request.AlipayOpenAppQrcodeCreateRequest;
import com.alipay.api.response.AlipayOpenAppQrcodeCreateResponse;
import com.rytx.common.config.MyAliPayConfig;
import com.rytx.common.core.domain.model.ArtUser;
import com.rytx.common.utils.DateUtils;
import com.rytx.common.utils.alipay.AlipayConfigUtil;
import com.rytx.system.mapper.ArtUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.rytx.system.mapper.ArtPartnerMapper;
import com.rytx.system.domain.ArtPartner;
import com.rytx.system.service.IArtPartnerService;

import javax.annotation.Resource;

import static com.rytx.common.utils.PageUtils.startPage;

/**
 * 合作商管理Service业务层处理
 *
 * @author zhangzb
 * @date 2024-09-04
 */
@Service
public class ArtPartnerServiceImpl implements IArtPartnerService
{
    @Autowired
    private ArtPartnerMapper artPartnerMapper;

    @Autowired
    private ArtUserMapper artUserMapper;

//    @Value("${alipay.appId}")
//    private String appId;
//
//    @Value("${alipay.privateKey}")
//    private String privateKey;
//
//    @Value("${alipay.alipayPublicKey}")
//    private String alipayPublicKey;
//
//    @Value("${alipay.gatewayUrl}")
//    private String gatewayUrl;

    @Resource
    private MyAliPayConfig aliPayConfig;

    /**
     * 查询合作商管理
     *
     * @param id 合作商管理主键
     * @return 合作商管理
     */
    @Override
    public ArtPartner selectArtPartnerById(Long id)
    {
        return artPartnerMapper.selectArtPartnerById(id);
    }

    /**
     * 查询合作商管理列表
     *
     * @param artPartner 合作商管理
     * @return 合作商管理
     */
    @Override
    public List<ArtPartner> selectArtPartnerList(ArtPartner artPartner)
    {
        return artPartnerMapper.selectArtPartnerList(artPartner);
    }

    /**
     * 新增合作商管理
     *
     * @param artPartner 合作商管理
     * @return 结果
     */
    @Override
    public int insertArtPartner(ArtPartner artPartner)
    {
        artPartner.setCreateTime(DateUtils.getNowDate());
        return artPartnerMapper.insertArtPartner(artPartner);
    }

    /**
     * 修改合作商管理
     *
     * @param artPartner 合作商管理
     * @return 结果
     */
    @Override
    public int updateArtPartner(ArtPartner artPartner)
    {
        artPartner.setUpdateTime(DateUtils.getNowDate());
        return artPartnerMapper.updateArtPartner(artPartner);
    }

    /**
     * 批量删除合作商管理
     *
     * @param ids 需要删除的合作商管理主键
     * @return 结果
     */
    @Override
    public int deleteArtPartnerByIds(Long[] ids)
    {
        return artPartnerMapper.deleteArtPartnerByIds(ids);
    }

    /**
     * 删除合作商管理信息
     *
     * @param id 合作商管理主键
     * @return 结果
     */
    @Override
    public int deleteArtPartnerById(Long id)
    {
        return artPartnerMapper.deleteArtPartnerById(id);
    }

    /**
     * 查询合作商用户列表
     * @param artUser
     * @return
     */
    @Override
    public List<ArtUser> selectArtUserList(ArtUser artUser) {
        startPage();
        return artUserMapper.selectArtUserListByNickName(artUser);
    }

    @Override
    public boolean bindZfbUser(Map<String, Object> map) {
        System.out.println(JSON.toJSONString(map));
        ArtPartner artPartner = new ArtPartner();
        if(ObjectUtil.isEmpty(map.get("id"))) {
            throw new RuntimeException("合作商ID不能为空");
        }
        if(ObjectUtil.isEmpty(map.get("userId"))) {
            throw new RuntimeException("用户ID不能为空");
        }
        artPartner.setId(Long.parseLong(map.get("id").toString()));
        ArtUser artUser = artUserMapper.selectArtUserById(Long.parseLong(map.get("userId").toString()));
        if(ObjectUtil.isEmpty(artUser)) {
            throw new RuntimeException("用户不存在");
        }
        ArtUser artUser1 = new ArtUser();
        artUser1.setId(artUser.getId());
        artUser1.setPartnersId(artPartner.getId());
        return artUserMapper.updateArtUser(artUser1) > 0;
    }

    @Override
    public String generateQRCode( String partnerId) {
        try {
// 初始化SDK
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfigUtil.init(aliPayConfig));

            // 构造请求参数以调用接口
            AlipayOpenAppQrcodeCreateRequest request = new AlipayOpenAppQrcodeCreateRequest();
            AlipayOpenAppQrcodeCreateModel model = new AlipayOpenAppQrcodeCreateModel();
            // 设置跳转小程序的页面路径
            model.setUrlParam("pages/index/index");
            // 设置小程序的启动参数
            model.setQueryParam("partnerId=" + partnerId);
            // 设置码描述
            model.setDescribe("绑定小程序用户");
            request.setBizModel(model);
            // 第三方代调用模式下请设置app_auth_token
            // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");
            AlipayOpenAppQrcodeCreateResponse response = alipayClient.execute(request);
            if (response.isSuccess()) {
                return response.getQrCodeUrlCircleBlue();
            } else {
                throw new RuntimeException("生成二维码失败: " + response.getSubMsg());
            }
        } catch (Exception e) {
            throw new RuntimeException("生成二维码失败: " + e.getMessage());
        }
    }

}
